package com.zzy.mccjt.gateway.common.filter;

import com.zzy.mccjt.gateway.common.FilterOrders;
import com.zzy.mccjt.gateway.common.filter.privacy.AbstractPrivacyHandle;
import com.zzy.mccjt.gateway.common.filter.privacy.PrivacyDecryptHandle;
import com.zzy.mccjt.gateway.common.properties.PrivacyHandleProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.factory.AbstractChangeRequestUriGatewayFilterFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.Map;
import java.util.Optional;

/**
 * description:接口入参处理:  处理URL 地址上的参数-->> 对加密数据进行解密
 *
 * @author zzy
 * @version 1.0
 * @date 2020/4/3
 */
@Slf4j
@Component
public class DecryptRequestParameterFilter /*extends AbstractChangeRequestUriGatewayFilterFactory*/ {

    /*@PostConstruct
    public void init(){
        log.info("DecrypRequestParameterFilter is ready to inject, order: {}",1);
    }
    public DecryptRequestParameterFilter() {
            super(String.class, FilterOrders.DECRYP_FILTER);
    }
    @Override
    protected Optional<URI> determineRequestUri(ServerWebExchange exchange, Object config) {

        ServerHttpRequest serverHttpRequest= exchange.getRequest();
        HttpMethod httpMethod=serverHttpRequest.getMethod();
        URI uri =serverHttpRequest.getURI();
        if(HttpMethod.GET.equals(httpMethod)){
            //获取查询参数
            String path=serverHttpRequest.getPath().toString();
            //解密数据
            AbstractPrivacyHandle privacyHandle = SpringContextHolder.getBean(PrivacyDecryptHandle.class);
            PrivacyHandleProperties.HandleTarget target = privacyHandle.checkIsHandle(path);
            if (target == null) {
                //未配置,不进行数据解密处理
                return Optional.of(uri);
            }
            String originalQuery = uri.getRawQuery();
            JSONObject parmObject=new JSONObject();
            if (StringUtils.isNotBlank(originalQuery)) {
                String [] parmsList=originalQuery.split("&");
                for(String parm: parmsList){
                    String []  nameValue=parm.split("=");
                    parmObject.put(nameValue[0],nameValue[1]);
                }
            }
            //解密数据
            String newParmJsonStr=privacyHandle.getData(path, parmObject.toJSONString(),target);
            //生成新的URI
            JSONObject newParmObject= JSONObject.parseObject(newParmJsonStr);
            StringBuilder query = new StringBuilder();
            for(Map.Entry<String, Object> entry:newParmObject.entrySet()){
                if(!StringUtils.isBlank(query.toString())){
                    query.append("&");
                }
                query.append(entry.getKey());
                query.append('=');
                query.append(entry.getValue());
            }
            try {

                URI newUri = UriComponentsBuilder.fromUri(uri)
                        .replaceQuery(query.toString())
                        .build(true)
                        .toUri();
                return Optional.of(newUri);

            } catch (RuntimeException ex) {
                throw new IllegalStateException("Invalid URI query: \"" + originalQuery + "\"");
            }
        }
        return Optional.of(uri);
    }*/
}
